﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using HNQXYJ.Model;

namespace HNQXYJ.DAL
{
    public class AreaGroupDAL
    {
        private AreaGroup ToModel(DataRow dr)
        {
            AreaGroup ag = new AreaGroup();
            ag.ID = Convert.ToInt32(dr["Id"]);
            ag.Name = dr["Name"].ToString();
            ag.AreaCode = dr["AreaCode"].ToString();
            ag.RankLevel = dr["RankLevel"].ToString();
            ag.DateAdd = DateTime.Now;
            ag.Notes = dr["Notes"].ToString();

            return ag;
        }
      
       /// <summary>
        /// 获取areaCode的最大值 如果返回-1说明表中还没有这个权限下的分组
       /// </summary>
       /// <param name="ranklevel"></param>
       /// <returns></returns>
        public int GetAreaCodeMax(string ranklevel)
        {
            string procName = "AreaGroupGetAreaCodeMax";
            SqlParameter[] paras = { 
                                   new SqlParameter("@RankLevel",SqlDbType.VarChar,100)
                                   };
            paras[0].Value = ranklevel;
            object m1= SqlHelper.ExecuteScalar(procName,paras);
            if (m1 == DBNull.Value)
            {
                return -1;
            }
            else
            {
                return Convert.ToInt32(m1);
            }
        }

        /// <summary>
        /// 向分组表中添加数据
        /// </summary>
        /// <param name="ag"></param>
        /// <returns></returns>
        public int AddNewGroup(AreaGroup ag)
        {
            string procName = "AreaGroupAddData";
            SqlParameter[] paras = { 
                                   new SqlParameter("@Name",SqlDbType.NVarChar,500),
                                   new SqlParameter("@AreaCode",SqlDbType.VarChar,100),
                                   new SqlParameter("@RankLevel",SqlDbType.VarChar,100),
                                   new SqlParameter("@DateAdd",SqlDbType.DateTime),
                                   new SqlParameter("@Notes",SqlDbType.NVarChar,500)
                                   };
            paras[0].Value = ag.Name;
            paras[1].Value = ag.AreaCode;
            paras[2].Value = ag.RankLevel;
            paras[3].Value = ag.DateAdd;
            paras[4].Value = ag.Notes;

            int res = SqlHelper.ExecNonQuery(procName, paras);
            return res;
        }

        /// <summary>
        /// 根据权限码回去分组列表
        /// </summary>
        /// <param name="ranklevel"></param>
        /// <returns></returns>
        public List<AreaGroup> GetListByRankLevel(string ranklevel)
        {
            string procName = "AreaGroupGetListByRankLevel";
            SqlParameter[] paras = { 
                                   new SqlParameter("@RankLevel",SqlDbType.VarChar,100)
                                   };
            paras[0].Value = ranklevel;

            DataTable dt = SqlHelper.GetDataTable(procName, paras);
            if (dt.Rows.Count > 0)
            {
                List<AreaGroup> list = new List<AreaGroup>();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(ToModel(dr));
                }
                return list;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据id删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDataById(int id)
        {
            string procName = "AreaGroupDeleteDataById";
            SqlParameter[] paras = {
                                   new SqlParameter("@Id",SqlDbType.Int)
                                   };
            paras[0].Value = id;
            int res = SqlHelper.ExecNonQuery(procName, paras);
            return res;
        }

        /// <summary>
        /// 根据id获取用户对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AreaGroup GetDataById(int id)
        {
            AreaGroup ag = new AreaGroup();
            string procName = "AreaGroupGetDataById";
            SqlParameter[] paras = {
                                   new SqlParameter("@Id",SqlDbType.Int)
                                   };
            paras[0].Value = id;
            DataTable dt = SqlHelper.GetDataTable(procName, paras);
            ag = ToModel(dt.Rows[0]);
            return ag;
        }

        /// <summary>
        /// 根据id更新数据
        /// </summary>
        /// <param name="ag"></param>
        /// <returns></returns>
        public int UpdateById(AreaGroup ag)
        {
            string procName = "AreaGroupUpdateDataById";
            SqlParameter[] paras = {
                                   new SqlParameter("@Id",SqlDbType.Int),
                                   new SqlParameter("@Name",SqlDbType.NVarChar,50),
                                   new SqlParameter("@Notes",SqlDbType.NVarChar,500)
                                   };
            paras[0].Value = ag.ID;
            paras[1].Value = ag.Name;
            paras[2].Value = ag.Notes;

            int res = SqlHelper.ExecNonQuery(procName, paras);
            return res;
 
        }
    }
}